package com.liu.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    public Queue deadQueue() {
        return QueueBuilder.durable("dead-queue").build();
    }


    //设置队列方式一
 /*   public Queue queue(){
        Map<String,Object> map = new HashMap<>();
        map.put("x-dead-letter-exchange","dead-ex");//指明死信交换器
        map.put("x-dead-letter-routing-key","dead");//绑定死信路由键
        //设置消息过期时间
        map.put("x-message-ttl",10000);//单位ms
        return  new Queue("queue",true,false,false,map);
    }*/
    //设置队列方式二
    @Bean
    public Queue queue() {
        return QueueBuilder.durable("queue")
                .deadLetterExchange("dead-ex")
                .deadLetterRoutingKey("dead")
                .ttl(10000) //过期时间
                .build();
    }

    //正常交换器
    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("normal-ex");
    }

    //死信交换器  其实就是交换器名字不同
    @Bean
    public DirectExchange deadExchange() {
        return new DirectExchange("dead-ex");
    }

    //绑定正常交换器
    @Bean
    public Binding binding(DirectExchange directExchange, Queue queue) {
        return BindingBuilder.bind(queue).to(directExchange).with("normal");
    }

    //绑定死信交换器
    @Bean
    public Binding bindingDead(DirectExchange directExchange, Queue deadQueue) {
        return BindingBuilder.bind(deadQueue).to(directExchange).with("dead");
    }

}
